﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PankowGame13
{
    class FloatingTextManager
    {
        List<FloatingText> floatingTexts = new List<FloatingText>();

        /// <summary>
        /// How many active FloatingTexts belong to this manager.
        /// </summary>
        public int Count { get { return floatingTexts.Count; } }

        /// <summary>
        /// The Color of the FloatingTexts that this manager manages. Can be changed during use.
        /// </summary>
        public Color Color { get { return color; } set { color = value; }  }
        private Color color;

        private bool randomColor = false;
        private Random r;

        /// <summary>
        /// The Font of the FloatingTexts that this manager manages. Can be changed during use.
        /// </summary>
        public SpriteFont Font { get { return font; } set { font = value; } }
        private SpriteFont font;

        /// <summary>
        /// FloatingTextManager constructor.
        /// To use:
        /// 1. Create a new FloatingTextManager  FloatingTextManager ftm = new FloatingTextManager(font,color);
        /// 2. Update() during the update phase  ftm.Update(gameTime);
        /// 3. Draw() during the draw            ftm.Draw(spriteBatch);
        /// 4. Add new FloatingTexts to manager  ftp.AddFloatingText(position,text,ttl);
        /// If you read this, you win the game. Come to Mr P or Mr G for a prize.
        /// </summary>
        /// <param name="font">SpriteFont to use for all of the FloatingTexts added. (This property can be changed)</param>
        /// <param name="color">Color to use for all of the FloatingTexts added. (This property can be changed)</param>
        public FloatingTextManager(SpriteFont font, Color color)
        {
            this.font = font;
            this.color = color;
        }

        /// <summary>
        /// FloatingTextManager constructor. Color of text will be random each time. (Color can NOT be changed when
        /// the manager is created in this way)
        /// To use:
        /// 1. Create a new FloatingTextManager  FloatingTextManager ftm = new FloatingTextManager(font,color);
        /// 2. Update() during the update phase  ftm.Update(gameTime);
        /// 3. Draw() during the draw            ftm.Draw(spriteBatch);
        /// 4. Add new FloatingTexts to manager  ftp.AddFloatingText(position,text,ttl);
        /// If you read this, you win the game. Come to Mr P or Mr G for a prize.
        /// </summary>
        /// <param name="font">SpriteFont to use for all of the FloatingTexts added. (This property can be changed)</param>
        public FloatingTextManager(SpriteFont font)
        {
            this.font = font;
            randomColor = true;
            r = new Random();
        }

        /// <summary>
        /// Add a new FloatingText to this FloatingTextManager
        /// </summary>
        /// <param name="position">Where you want the text to start on the screen</param>
        /// <param name="text">What you want the floating text to say</param>
        /// <param name="ttl">How long (in milliseconds) you want the text to float upwards</param>
        public void AddFloatingText(Vector2 position, String text, Color c, int ttl)
        {
            floatingTexts.Add(new FloatingText(position, text, c, ttl));
        }

        /// <summary>
        /// Updates all of the FloatingTexts that belong to this FloatingTextManager.
        /// </summary>
        /// <param name="gameTime">Current gameTime to determine time elapsed for updating each FloatingTexts time-to-live.</param>
        public void Update(GameTime gameTime, Game1 game)
        {

            //if (floatingTexts.Count > 0) game.Window.Title = floatingTexts[0].Fade.ToString();
            for (int i = 0; i < floatingTexts.Count(); i++)
            {
                if (floatingTexts[i].Alive != true)
                {
                    floatingTexts.Remove(floatingTexts[i]);
                    i--;
                }
                else
                {
                    floatingTexts[i].Update(gameTime);
                }
            }
        }


        /// <summary>
        /// Draws all of the FloatingTexts that belong to this FloatingTextManager.
        /// </summary>
        /// <param name="sb">Pass an already begun SpriteBatch.</param>
        public void Draw(SpriteBatch sb)
        {
            Color c;
            if (randomColor == true)
            {
                c = new Color(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256));
            }
            else
            {
                c = color;
            }
            foreach (FloatingText ft in floatingTexts)
            {
                ft.Draw(sb, font);
            }
        }
    }
}
